export const formatDate = (date, formatString = 'yyyy-MM-dd HH:mm:ss') => {
  const map = {
    yyyy: date.getFullYear(),
    MM: ('0' + (date.getMonth() + 1)).slice(-2),
    dd: ('0' + date.getDate()).slice(-2),
    HH: ('0' + date.getHours()).slice(-2),
    mm: ('0' + date.getMinutes()).slice(-2),
    ss: ('0' + date.getSeconds()).slice(-2)
  }

  return formatString.replace(/yyyy|MM|dd|HH|mm|ss/g, (matched) => map[matched])
}

export const generateUUID = () => {
  // 判断是否支持crypto模块
  if (typeof crypto === 'undefined') {
    throw new Error('crypto module not available')
  }

  // 创建一个数组来存储UUID的各个部分
  const uuidArray = new Array(36).fill(0)

  // 生成随机数并填充到数组中
  crypto.getRandomValues(new Uint8Array(uuidArray.buffer))

  // 设置UUID的特定格式
  uuidArray[8] = uuidArray[13] = uuidArray[18] = uuidArray[23] = '-'
  uuidArray[14] = '4'

  // 将数组转换为字符串并返回
  return uuidArray.join('')
}
